module AboutYou
  module SDK
    module Model
      ###
      # This class represents a variants result
      ###
      class VariantsResult
        # Array of AboutYou::SDK::Model::Variant
        attr_accessor :variants
        # Array of Errors
        attr_accessor :errors

        ###
        # This method is used for creating the variants result
        #
        # * *Args*    :
        #   - +variants+ -> Array of AboutYou::SDK::Model::Variant 
        #   - +errors+ -> Array of Errors [optional]
        #   - +product_search_result+ -> AboutYou::SDK::Model::ProductSearchResult [optional]
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::VariantsResult
        ###
        def self.create(variants, errors = [], product_search_result)
          variants_result = new
          variants_result.variants = {}
          variants_result.errors = errors
          # no variant was found
          return variants_result if
          product_search_result == false || variants.count == 0

          # get products from product-search
          products = product_search_result.products
          variants.each do |variant_id, product_id|
            unless products[product_id]
              # product was not delivered
              variants_result.errors.push(variant_id)
              next
            end

            product = products[product_id]
            variant = product.variant_by_id(variant_id)

            variants_result.variants[variant_id] = variant if
            variant.is_a?(AboutYou::SDK::Model::Variant)
          end

          variants_result
        end

        ###
        # This method checks if there are variants found
        #
        # * *Returns* :
        #   - Boolean determining whether the result has found variants or not
        ###
        def variants_found?
          variants.count > 0
        end

        ###
        # This method checks if there are variants not found
        #
        # * *Returns* :
        #   - Boolean determining whether the result has not found variants or not
        ###
        def variants_not_found?
          errors.count > 0
        end

        ###
        # Getter for the found variants
        #
        # * *Returns* :
        #   - Array of AboutYou::SDK::Model::Variant
        ###
        def variants_found
          variants
        end

        ###
        # This method searches for a variant by a certain variant_id
        #
        # * *Args* :
        #   - +id+ -> id used for searching 

        # * *Returns* :
        #   - nil / instance of AboutYou::SDK::Model::Variant
        ###
        def variant_by_id(id)
          variants[id] ? variants[id] : nil
        end

        ###
        # geter for the not found variants
        #
        # * *Returns* :
        #   - Array of AboutYou::SDK::Model::Variant
        ###
        def variants_not_found
          errors
        end
      end
    end
  end
end
